package org.jsbd.boss.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.dao.IBaseInfoDAO;
import org.jsbd.boss.domian.BaseInfo;
import org.jsbd.boss.domian.channel.IssueBaseStatistics;
import org.jsbd.boss.domian.channel.IssueBaseStatisticsVo;
import org.jsbd.boss.domian.channel.IssueLoginStatistics;
import org.jsbd.boss.domian.channel.IssueLoginStatisticsVo;
import org.jsbd.boss.domian.channel.IssueStatistics;
import org.jsbd.boss.domian.channel.IssueStatisticsVo;
import org.jsbd.boss.domian.channel.SimulatorSysVer;
import org.jsbd.boss.service.IBaseInfoService;
import org.jsbd.boss.service.IIssueStatisticsService;
import org.jsbd.boss.service.ISimulatorSysVerService;
import org.jsbd.boss.util.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/issueStatistics")
public class IssueStatisticsController {

  private static final Logger logger = LoggerFactory.getLogger(IssueStatisticsController.class);

  @Autowired
  private IIssueStatisticsService issueStatisticsService;
  @Resource
  private ISimulatorSysVerService sysVerService;
  @Resource
  private IBaseInfoService baseInfoService;

  /**
   * 下发量统计页面
   * @return
   */
  	@RequestMapping(value = "/")
  	public String daylimit(Model model) {
  		List<SimulatorSysVer> simulatorSysVers = sysVerService.selectSimulatorSysVer(null);
  		model.addAttribute("simulatorSysVers", simulatorSysVers);
  		return "issueStatistics/list";
  	}
  
  	/**
  	 * 下发量统计数据
  	 * @param model
  	 * @param record
  	 * @return
  	 */
  	@RequestMapping(value = "list", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> list(Model model, IssueStatisticsVo issueStatisticsVo) {
  		String startTime = issueStatisticsVo.getStartTime();
  		String endTime = issueStatisticsVo.getEndTime();
  		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
  			issueStatisticsVo.setStartTime(DateUtil.getHourBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD_HHMMSS, 2));
  		}
  		if (StringUtils.isBlank(startTime) && !StringUtils.isBlank(endTime)) {
  			
  			issueStatisticsVo.setStartTime(DateUtil.getHourBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD_HHMMSS, 2));
  		}
  		
  		Map<String, Object> map = new HashMap<String, Object>();
  		List<String> xAxis = issueStatisticsService.selectMinute(issueStatisticsVo);
		List<IssueStatistics> yAxis = issueStatisticsService.selectRecords(issueStatisticsVo);
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);
		return map;
	}
  	
  	/**
     * 下发量统计页面-登录类型
     * @return
     */
	@RequestMapping(value = "/login")
	public String issueLoginStatistics(Model model) {
		List<SimulatorSysVer> simulatorSysVers = sysVerService.selectSimulatorSysVer(null);
		model.addAttribute("simulatorSysVers", simulatorSysVers);
		return "issueStatistics/loginTypeList";
	}
	
	/**
  	 * 下发量统计数据-登录类型
  	 * @param model
  	 * @param record
  	 * @return
  	 */
  	@RequestMapping(value = "loginList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> loginList(Model model, IssueLoginStatisticsVo issueLoginStatisticsVo) {
  		String startTime = issueLoginStatisticsVo.getStartTime();
  		String endTime = issueLoginStatisticsVo.getEndTime();
  		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
  			issueLoginStatisticsVo.setStartTime(DateUtil.getHourBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD_HHMMSS, 2));
  		}
  		
  		Map<String, Object> map = new HashMap<String, Object>();
  		List<String> xAxis = issueStatisticsService.selectLoginMinute(issueLoginStatisticsVo);
		List<IssueLoginStatistics> yAxis = issueStatisticsService.selectLoginRecords(issueLoginStatisticsVo);
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);
		return map;
	}
  	
  	/**
     * 下发量统计页面-基地
     * @return
     */
	@RequestMapping(value = "/base")
	public String issueBaseStatistics(Model model) {
		List<SimulatorSysVer> simulatorSysVers = sysVerService.selectSimulatorSysVer(null);
		List<BaseInfo> baseInfos = baseInfoService.getAllChannelInfo();
		model.addAttribute("simulatorSysVers", simulatorSysVers);
		model.addAttribute("baseInfos", baseInfos);
		return "issueStatistics/baseTypeList";
	}
	
	/**
  	 * 下发量统计数据-基地
  	 * @param model
  	 * @param record
  	 * @return
  	 */
  	@RequestMapping(value = "baseList", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> baseList(Model model, IssueBaseStatisticsVo issueBaseStatisticsVo) {
  		String startTime = issueBaseStatisticsVo.getStartTime();
  		String endTime = issueBaseStatisticsVo.getEndTime();
  		if (StringUtils.isBlank(startTime) && StringUtils.isBlank(endTime)) {
  			issueBaseStatisticsVo.setStartTime(DateUtil.getDayBeforeDate(DateUtil.DATE_PATTEN_YY_MM_DD, 30));
  		}
  		
  		Map<String, Object> map = new HashMap<String, Object>();
  		List<String> xAxis = issueStatisticsService.selectBaseDays(issueBaseStatisticsVo);
		List<IssueBaseStatistics> yAxis = issueStatisticsService.selectBaseRecords(issueBaseStatisticsVo);
		map.put("xAxis", xAxis);
		map.put("yAxis", yAxis);
		return map;
	}
  	
  	
  	

}
